package pers.qtong.tank.cor;

import pers.qtong.tank.Bullet;
import pers.qtong.tank.Explode;
import pers.qtong.tank.GameObject;
import pers.qtong.tank.Tank;

public class BulletTankCollider implements Collider {
    @Override
    public boolean collide(GameObject o1, GameObject o2) {
        if(o1 instanceof Bullet && o2 instanceof Tank) {
            Bullet bullet = (Bullet)o1;
            Tank tank = (Tank)o2;
            if(bullet.getGroup() == tank.getGroup()) {
                return true;
            }
            if(bullet.rectangle.intersects(tank.rectangle)) {
                bullet.die();
                tank.die();
                int ex = tank.getX() + Tank.WIDTH / 2 - Bullet.width / 2;
                int ey = tank.getY() + Tank.HEIGHT / 2 - Bullet.height / 2;
                new Explode(ex, ey);
                return false;
            }
            return true;
        }else if(o1 instanceof Tank && o2 instanceof Bullet) {
            collide(o2, o1);
        }
        return true;
    }
}
